<?php
/**
 * Editor controller
 *
 * @author bigjohn
 *
 */

class EditorController extends modules_default_controllers_ControllerBase
{
	public $ajaxable = array(
		'crop' => true,
	);
		
	/*
	 * @todo !!!!!!!!!!!!!!!!!!!! change later
	 */
	protected $_bForLoggedUsersOnly = false;

	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			parent::init();
			$this->view->title = 'Edit Card';
		}
		
		$this->_helper->ajaxContext()->initContext('json');		
	}

	public function indexAction()
	{
		$this->view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui-1.7.2.custom.css');
		$this->view->headLink()->appendStylesheet('/css/colorpicker/colorpicker.css');
//		$this->view->headLink()->appendStylesheet('/css/jquery.jcrop.css');
		$this->view->headLink()->appendStylesheet('/css/imgareaselect.css');
		$this->view->headLink()->appendStylesheet('/css/editor.css');

		$this->view->headScript()->appendFile('/js/lib/jquery-ui.js');
		$this->view->headScript()->appendFile('/js/lib/swfobject.js');
		$this->view->headScript()->appendFile('/js/lib/jquery.uploadify.v2.1.0.min.js');
		$this->view->headScript()->appendFile('/js/lib/colorpicker/colorpicker.js');
//		$this->view->headScript()->appendFile('/js/lib/jquery.jcrop.min.js');
		$this->view->headScript()->appendFile('/js/lib/jquery.imgareaselect.pack.js');
		$this->view->headScript()->appendFile('/js/editor.js');
		
		$oRequest 		= $this->getRequest();
		$idCard 		= (int)$oRequest->getParam('card');
		$idSize 		= (int)$oRequest->getParam('size');
		$this->view->idCard	= $idCard;
		$this->view->idSize	= $idSize;
	}
	
	public function imguploadAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		file_put_contents('/var/log/test.log', print_r($_FILES, 1), FILE_APPEND);
		
		$response = array();
		$error = null;
		$file = null;

		if (!empty($_FILES) && !empty($_FILES['image']))
		{
			$tempFile = $_FILES['image']['tmp_name'];
			$fileParts  = pathinfo($_FILES['image']['name']);
			
			$size = @getimagesize($tempFile);
			
			if (! in_array($size[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)))
			{
				$error = 'Invalid image file';				
			}
			else
			{
				$typesArray = array('jpg', 'gif', 'png');
				
				if (in_array($fileParts['extension'], $typesArray))
				{
					$file = '/i_tmp/' . md5(microtime()) . '.' . $fileParts['extension'];
										
					move_uploaded_file($tempFile, $_SERVER['DOCUMENT_ROOT'] . $file);
				}
				else
				{
					$error = 'Invalid file type';
				}
			}
		}
		else
		{
			$error = 'Upload error';
		}
		
		$response['status'] = empty($error);
		$response['file']   = $file;
		$response['error']  = $error;		
		
		echo json_encode($response);
	}	
	
	public function cropAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
//		file_put_contents('/var/log/test.log', print_r($_REQUEST, 1), FILE_APPEND);
		
		$response = array();
		$error = null;
		$file = null;
		
		$fileParts  = pathinfo($_SERVER['DOCUMENT_ROOT'] . $_REQUEST['image']);
		
		$format = ('jpg' == $fileParts['extension'] ? 'jpeg' : $fileParts['extension']);

		/*
		 * crop image
		 */
		$function = 'imagecreatefrom' . $format;
		$source = $function($_SERVER['DOCUMENT_ROOT'] . $_REQUEST['image']);
		
		$target = imageCreateTrueColor($_REQUEST['width'], $_REQUEST['height']);
		
		imageCopyResampled(
			$target, $source,
			0,0,
			$_REQUEST['x'], $_REQUEST['y'],
			$_REQUEST['width'], $_REQUEST['height'],
			$_REQUEST['width'], $_REQUEST['height']
		);
		
		$target_path = '/i_tmp/' . md5(microtime()) . '.' . $fileParts['extension'];

		$function = 'image' . $format;
		$function($target, $_SERVER['DOCUMENT_ROOT'] . $target_path, 75);
		
		$file = $target_path;
		
		$this->view->status = empty($error);
		$this->view->file   = $file;
		$this->view->error  = $error;		
	}	

}
